package freshmanspecial.mredrock.com.welcomefreshman.Model;

import android.util.Log;

import freshmanspecial.mredrock.com.welcomefreshman.Bean.BeautyInNearBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.CanteenBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.CateBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.DormitoryBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.FailRatioBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.LifeInNearBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.QQGroupBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.SchoolBuildingsBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.SexRatioBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.WorkRatioBean;
import freshmanspecial.mredrock.com.welcomefreshman.Service.DataService;
import freshmanspecial.mredrock.com.welcomefreshman.Service.StrategyService;
import freshmanspecial.mredrock.com.welcomefreshman.View.IStrategyView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by HP on 2017/8/12.
 */

public class StrategyModel implements IStrategyModel{
    private String api = "http://www.yangruixin.com/";

    public interface OnGetData{
        void onFinish(Object dataBean);
    }
    @Override
    public void getSchoolBuildings(final StrategyModel.OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        StrategyService service = retrofit.create(StrategyService.class);
        service.getSchoolBuildings("SchoolBuildings").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SchoolBuildingsBean>() {
                    SchoolBuildingsBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SchoolBuildingsBean value) {
                        Log.d("12333",value.getData().get(0).getTitle());

                        bean = value;
                    }



                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }

    @Override
    public void getDormitory(final StrategyModel.OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        StrategyService service = retrofit.create(StrategyService.class);
        service.getDormitory("Dormitory").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DormitoryBean>() {
                    DormitoryBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(DormitoryBean value) {
                        bean = value;
                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }

    @Override
    public void getCanteen(final StrategyModel.OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        StrategyService service = retrofit.create(StrategyService.class);
        service.getCanteen("Canteen").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CanteenBean>() {
                    CanteenBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CanteenBean value) {
                        bean = value;
                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }

    @Override
    public void getQQgroup(final OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        StrategyService service = retrofit.create(StrategyService.class);
        service.getQQgroup("QQGroup").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<QQGroupBean>() {
                    QQGroupBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(QQGroupBean value) {
                        bean = value;
                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }

    public void getLife(final StrategyModel.OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        StrategyService service = retrofit.create(StrategyService.class);
        service.getLifeInNear("LifeInNear").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LifeInNearBean>() {
                    LifeInNearBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LifeInNearBean value) {
                        bean = value;
                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }
    public void getMeijing(final StrategyModel.OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        StrategyService service = retrofit.create(StrategyService.class);
        service.getBeautyInNear("BeautyInNear").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BeautyInNearBean>() {
                    BeautyInNearBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BeautyInNearBean value) {
                        bean = value;
                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }
    public void getMeishi(final StrategyModel.OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        StrategyService service = retrofit.create(StrategyService.class);
        service.getCate("Cate").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CateBean>() {
                    CateBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CateBean value) {
                        bean = value;
                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }

}
